﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;

namespace SP_XHWeb.Layouts.SP_XHWeb
{
    public class RequestHelper
    {
        public string ServiceUrl { get; set; }

        public RequestHelper(string url)
        {
            ServiceUrl = url;
        }

        public string Get(params object[] parameterValues)
        {
            try
            {
                string url = ServiceUrl;
                if (url.Contains("?") == false) url += "?";

                //url = url+"&action=" + method;

                for (int i = 0; i < parameterValues.Length; i = i + 2)
                {
                    url = url + "&" + HttpUtility.UrlEncode(parameterValues[i].ToString()) + "=";
                    if (i + 1 < parameterValues.Length)
                    {
                        url = url + HttpUtility.UrlEncode(parameterValues[i + 1].ToString());
                    }
                }

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                //request.ContentType = "application/x-www-form-urlencoded";

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new WebException(response.StatusDescription);
                }
                StreamReader sr = new StreamReader(response.GetResponseStream());


                string result = sr.ReadToEnd();
                return result;

            }
            catch (Exception e)
            {
                throw e;
            }

        }

        public string Post(params object[] parameterValues)
        {
            try
            {
                string url = ServiceUrl;
                string data = "";

                //data = "&action=" + method;

                for (int i = 0; i < parameterValues.Length; i = i + 2)
                {
                    data = data + "&" + HttpUtility.UrlEncode(parameterValues[i].ToString()) + "=";
                    if (i + 1 < parameterValues.Length)
                    {
                        data = data + HttpUtility.UrlEncode(parameterValues[i + 1].ToString());
                    }
                }

                byte[] bdata = Encoding.UTF8.GetBytes(data);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = bdata.Length;
                Stream postStream = request.GetRequestStream();
                postStream.Write(bdata, 0, bdata.Length);
                postStream.Close();


                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new WebException(response.StatusDescription);
                }
                StreamReader sr = new StreamReader(response.GetResponseStream());


                string result = sr.ReadToEnd();
                return result;

            }
            catch (Exception e)
            {
                throw e;
            }

        }
    }
}
